<?php $this->_extends('_layouts/main_layout'); ?>
<?php $this->_block('title'); ?>
	Fulfillments
<?php $this->_endblock(); ?>

<?php $this->_block('contents'); ?>
<div id="content" class="box">
	<h1>Fulfillments</h1>
		<?php if (isset($infos)) 
				{
					foreach ($infos as $info)
					{
		?>
				<p class="msg info"><?php echo $info;?></p>
		<?php		}
				}
		?>
		<?php if (isset($errors)) 
				{
					foreach ($errors as $error)
					{
		?>
				<p class="msg error"><?php echo $error;?></p>
		<?php		}
				}
		?>
	<fieldset>
		<legend>Search</legend>
		<form action="<?php echo url('fulfillment/index'); ?>" method="get">
			<div class="row" id="search">
				<select name="field">
                    <option value="email" <?php echo ('email'==$field) ? 'selected="selected"' : ''; ?>>Email</option>
					<option value="user_id" <?php echo ('user_id'==$field) ? 'selected="selected"' : ''; ?>>User ID</option>
					<option value="firstname" <?php echo ('firstname'==$field) ? 'selected="selected"' : ''; ?>>First Name</option>
					<option value="lastname" <?php echo ('lastname'==$field) ? 'selected="selected"' : ''; ?>>Last Name</option>
					<option value="status" <?php echo ('status'==$field) ? 'selected="selected"' : ''; ?>>Status</option>
				</select>
				<input type="text" name="condition" value="<?php echo isset($condition) ? $condition : ''; ?>"/>
				<script>
					$('select[name="field"]').change(function(){
						var fieldVal = $(this).val();
						if(fieldVal == 'status'){
							<?php $allStatus = Util::getAllFulfillmentStatus(); ?>
							$('[name="condition"]').replaceWith('<?php echo Util::buildDropDownList(array('name' => 'condition'), $allStatus, $condition) ?>');
						}else{
							$('[name="condition"]').replaceWith('<input type="text" name="condition" value="<?php echo isset($condition) ? $condition : ''; ?>" />');
						}
					});
					$('select[name="field"]').change();
				</script>
				<input type="submit" value="Search" />
			</div>
		</form>
	</fieldset>
	<table class="full-size">
		<tbody>
			<tr>
				<th>User Name</th>
				<th>Amount</th>
				<th>Status</th>
				<th>Created</th>
				<th>Updated</th>
				<th>Operate</th>
			</tr>
			<?php
				$bg = false;
				if(!empty($fulfillments)){
					foreach ($fulfillments as $fulfillment)
					{
			?>
			<tr <?php if ($bg) echo 'class="bg"';?>>
				<td><label style="text-decoration:underline; color:#06F; cursor:pointer;" class='un' id='<?php echo $fulfillment['uid']; ?>'><?php echo $fulfillment['name'];?></label></td>
				<td><?php echo $fulfillment['bill'];?></td>
				<td><?php echo Util::getFulfillmentStatus($fulfillment['status']);?></td>
				<td><?php echo date('m/d/Y H:i:s', $fulfillment['created']); ?></td>
				<td><?php echo date('m/d/Y H:i:s', $fulfillment['updated']); ?></td>
				<td>
					<?php if($fulfillment['status'] == 0): ?>
						<a href="<?php echo url('fulfillment/cancel',array('fid'=>$fulfillment['id'], 'hash'=>$fulfillment['uid']));?>">Cancel</a>
						<a href="<?php echo url('fulfillment/complete', array('fid'=>$fulfillment['id'], 'hash'=>$fulfillment['uid'])); ?>">Complete</a>
						<a href="<?php echo url('fulfillment/fraudulent', array('uid'=>$fulfillment['uid'])); ?>">Fraudulent</a>
					<?php elseif($fulfillment['status'] == 1): ?>
						<a href="<?php echo url('fulfillment/checkreturned', array('fid'=>$fulfillment['id'], 'hash'=>$fulfillment['uid'])); ?>">Check Returned</a>
					<?php elseif($fulfillment['status'] == 3): ?>
						<a href="<?php echo url('fulfillment/complete', array('fid'=>$fulfillment['id'], 'hash'=>$fulfillment['uid'])); ?>">Complete</a>
					<?php endif; ?>
				</td>
			</tr>
			<?php
					$bg = !$bg;
					}
				}
			?>
		</tbody>
	</table>

   	<table id='userinfo'>
        <tr>
            <th>Name:</th>
            <td id='userinfo_name'></td>
        </tr>
        <tr>
            <th>Email:</th>
            <td id='userinfo_email'></td>
        </tr>
        <tr>
            <th>Address1:</th>
            <td id='userinfo_addr1'></td>
        </tr>
        <tr>
            <th>Address2:</th>
            <td id='userinfo_addr2'></td>
        </tr>
        <tr>
            <th>Phone:</th>
            <td id='userinfo_phone'></td>
        </tr>
        <tr>
            <th>City:</th>
            <td id='userinfo_city'></td>
        </tr>
        <tr>
            <th>State:</th>
            <td id='userinfo_state'></td>
        </tr>
        <tr>
            <th>Zipcode:</th>
            <td id='userinfo_zipcode'></td>
        </tr>
        <tr>
            <th>Birthday:</th>
            <td id='userinfo_birthday'></td>
        </tr>
        <tr>
            <th>Gender:</th>
            <td id='userinfo_gender'></td>
        </tr>
        <tr>
            <th>Created:</th>
            <td id='userinfo_created'></td>
        </tr>
        <tr>
            <th>Last Visit:</th>
            <td id='userinfo_lv'></td>
        </tr>
        <tr>
            <th>Site:</th>
            <td id='userinfo_site'></td>
        </tr>
        <tr>
            <th>Credit:</th>
            <td id='userinfo_credit'></td>
        </tr>
        <tr>
            <th>Played:</th>
            <td id='userinfo_played'></td>
        </tr>
        <tr>
            <th>Won:</th>
            <td id='userinfo_won'></td>
        </tr>
        <tr>
            <th>Balance:</th>
            <td id='userinfo_balance'></td>
        </tr>
        <tr>
            <th>Offer Signed:</th>
            <td id='userinfo_offer'></td>
        </tr>
        <tr>
        	<th>Rewards Info:</th>
        	<td>
        		<table style="text-align:center;" id="userrewards">
        		</table>
        	</td>
        </tr>
    </table>
	<div class="pagination">
		<?php echo $pagination; ?>
	</div>
</div>

<script type="text/javascript">
	$('#userinfo').dialog({
		autoOpen: false,
		height: 'auto',
		width: 'auto',
		modal: true,
		buttons: {OK: function(){
						$( this ).dialog( "close" );		
					  }
				 },
	});

	$(".un").click(function(){
		$.ajax({
			type: 'GET',
			url: "<?echo url('user/getuserinfo'); ?>",
			data: {
					uid: $(this).attr('id'),
				  },
			success: function(data){
						data = eval('('+data+')');
						var userinfo = data['userinfo'];
						var user_rewards = data['user_rewards'];

						$("#userinfo_name").html(userinfo[0]['firstname'] + userinfo[0]['lastname']);
						$("#userinfo_email").html(userinfo[0]['email']);
						$("#userinfo_addr1").html(userinfo[0]['address1']);
						$("#userinfo_addr2").html(userinfo[0]['address2']);
						$("#userinfo_phone").html(userinfo[0]['phone']);
						$("#userinfo_city").html(userinfo[0]['city']);
						$("#userinfo_state").html(userinfo[0]['state']);
						$("#userinfo_zipcode").html(userinfo[0]['zipcode']);
						$("#userinfo_birthday").html(userinfo[0]['birthday']);
						$("#userinfo_gender").html( (userinfo[0]['zipcode']=='m' || userinfo[0]['zipcode']=='M') ? 'Male':'Female' );
						$("#userinfo_created").html(userinfo[0]['created']);
						$("#userinfo_lv").html(userinfo[0]['last_visit']);
						$("#userinfo_site").html(userinfo[0]['domain']);
						$("#userinfo_credit").html(userinfo[0]['credit']);
						$("#userinfo_played").html(userinfo[0]['played']);
						$("#userinfo_won").html("$"+userinfo[0]['won']);
						$("#userinfo_balance").html("$"+userinfo[0]['balance']);
						$("#userinfo_offer").html(userinfo[0]['offer']);

						$("#userrewards").html("<tr><th>Time&nbsp;&nbsp;<\/th><th>Name&nbsp;&nbsp;<\/th><th>Credit&nbsp;&nbsp;<\/th><\/tr>");
						for(var index in user_rewards){
							var rewardstr = "<tr>";
							for(var v in user_rewards[index]){
								rewardstr += ("<td>"+user_rewards[index][v]+"</td>");
							}
							rewardstr += "</tr>";
							$("#userrewards").append(rewardstr);
						}
						$('#userinfo').dialog('open');
					 }
		});
	});
	
	<?php if(isset($hash)): ?>
		window.location.hash = '<?php echo $hash; ?>';
	<?php endif; ?>
</script>

<?php $this->_endblock(); ?>